package servletUtenteReg;

import gestioneContesto.GestoreContesto;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logicaUtenteReg.GestoreProfiloRemote;

/**
 * Servlet implementation class DisiscrizioneServlet
 */
public class DisiscrizioneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private GestoreContesto contesto;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisiscrizioneServlet() {
        
    	super();
    	contesto = new GestoreContesto();
    	
    }

    /**
   	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   	 */
   	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   		
   		try {
   			eseguiRichiesta(request, response);
   		} catch (NamingException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
   		
   	}

   	/**
   	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   	 */
   	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   		
   		try {
   			eseguiRichiesta(request, response);
   		} catch (NamingException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
   		
   	}

   	private void eseguiRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {
   		
   		GestoreProfiloRemote gestore = contesto.getGestore(GestoreProfiloRemote.class, "GestoreProfiloRemote");
   		
   		String username = request.getParameter("username");
   		
   		//Elimino tutte le informazioni di quell'utente
   		gestore.disiscrizione(username);
   		
   		response.sendRedirect("Home.html");
	
   	}

}
